package org.chuan.auth.controller;

import org.chuan.auth.domain.Role;
import org.chuan.auth.dto.RoleDto;
import org.chuan.auth.query.RoleQuery;
import org.chuan.auth.service.IRoleService;
import org.chuan.base.annotation.SelfPermission;
import org.chuan.base.utils.AjaxResult;
import org.chuan.base.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
@SelfPermission(name = "角色管理")
public class RoleController {

    @Autowired
    public IRoleService roleService;


    /**
     * 保存和修改公用的
     * @param role  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @SelfPermission(name = "角色的新增和修改")
    public AjaxResult saveOrUpdate(@RequestBody Role role){
        try {
            if( role.getId()!=null)
                roleService.update(role);
            else
                roleService.save(role);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @SelfPermission(name = "角色删除")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            roleService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @SelfPermission(name = "查询一个角色")
    public AjaxResult queryById(@PathVariable("id")Long id)
    {
        try {
            Role role = roleService.queryById(id);
            return AjaxResult.me().setData(role);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    @SelfPermission(name = "查询全部角色")
    public AjaxResult queryAll(){

        try {
            List< Role> list = roleService.queryAll();
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @SelfPermission(name = "角色的分页查询")
    public AjaxResult queryPage(@RequestBody RoleQuery query)
    {
        try {
            PageList<Role> pageList = roleService.queryPage(query);
            return AjaxResult.me().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }

    @PutMapping("/saveOrUpdate")
    @SelfPermission(name = "角色的新增和修改")
    public AjaxResult addOrUpdate(@RequestBody RoleDto roleDto){
        try {
            roleService.addOrUpfate(roleDto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/per/{id}")
    @SelfPermission(name = "角色查询权限")
    public AjaxResult queryPermissionId(@PathVariable("id")Long id)
    {
        try {
            List<Long> ids = roleService.queryPermissionId(id);
            return AjaxResult.me().setData(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }
}
